﻿package mortal.game.view.copy.ChallengeCopy.view
{
    import com.gengine.global.*;
    import com.gengine.resource.*;
    import com.gengine.resource.info.*;
    import com.gengine.utils.*;
    import com.greensock.*;
    import com.greensock.layout.*;
    import com.mui.controls.*;
    import com.mui.core.*;
    import com.mui.display.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.text.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.scene3D.map3D.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.cd.*;
    import mortal.game.view.task.view.track.*;
    import mortal.mvc.core.*;

    public class ChallengeTimePanel extends View
    {
        private var _bgImg:ScaleBitmap;
        private var _moveBtn:GLoadedButton;
        private var _bgSprite:GSprite;
        private var _bodySprite:GSprite;
        private var _btnShowPanel:BtnShowTrackModule;
        private var _tween:TweenMax;
        private var _dragRect:Rectangle;
        public var _judgeRec:Rectangle;
        private var _leftTime:SecTimerCountView;
        private var _title:GButton;
        private var _timeBg:GBitmap;
        private var _line1:GSprite;
        private var _line2:GSprite;
        private var _line3:GSprite;
        private var _timeTxt:GTextFiled;
        private var _clip:MovieClip;
        private var _data:ChallengeTimeData;
        private var _isShowSkin:Boolean;
        private var _tweenPointer:TweenMax;
        private var _speed:Number;
        private static var _instance:ChallengeTimePanel;

        public function ChallengeTimePanel()
        {
            this._dragRect = new Rectangle();
            this._judgeRec = new Rectangle(0, 0, 240, 260);
            this.setSize(260, 300);
            if (_instance != null)
            {
                throw new Error(" ChallengeTimePanel 单例 ");
            }
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this.layer = LayerManager.uiLayer;
            this._bodySprite = UICompomentPool.getUICompoment(GSprite);
            this.addChild(this._bodySprite);
            this._bodySprite.mouseEnabled = false;
            this._bodySprite.mouseChildren = true;
            this._bgSprite = UICompomentPool.getUICompoment(GSprite);
            this._bodySprite.addChild(this._bgSprite);
            this._bgSprite.mouseEnabled = false;
            this._bgSprite.mouseChildren = true;
            this._timeBg = UIFactory.gBitmap("", 70, 80, this._bodySprite);
            this._bgImg = UIFactory.bg(0, 0, 260, 300, this._bgSprite, "TaskTrackBg");
            this._moveBtn = UIFactory.gLoadedButton(ImagesConst.TaskTrackMove_upSkin, 0, 0, 18, 18, this._bgSprite);
            this._moveBtn.toolTipData = HTMLUtil.addColor(Language.getString(20143), "#FFFFFF");
            this._btnShowPanel = UICompomentPool.getUICompoment(BtnShowTrackModule);
            this._btnShowPanel.width = 18;
            this._btnShowPanel.height = 18;
            this._btnShowPanel.x = 240;
            this._btnShowPanel.y = 0;
            this.addChild(this._btnShowPanel);
            this._btnShowPanel.configEventListener(MouseEvent.CLICK, this.showHideTrackHandler);
            this._title = UIFactory.gButton(Language.getString(35134), 20, 0, 220, 18, this._bodySprite);
            this.pushUIToDisposeVec(UIFactory.gTextField(Language.getString(35135), 50, 25, 100, 20, this._bodySprite, GlobalStyle.textFormatHuang));
            var _loc_1:* = new GTextFormat();
            _loc_1.size = 40;
            _loc_1.color = 15792122;
            _loc_1.align = AlignMode.CENTER;
            _loc_1.italic = true;
            _loc_1.letterSpacing = 5;
            this._timeTxt = UIFactory.gTextField("", 48, 115, 180, 70, this._bodySprite, _loc_1);
            this._leftTime = UIFactory.secTimeCountView(Language.getString(30056), 150, 25, 0, 0, this._bodySprite, GlobalStyle.textFormatHuang);
            this._leftTime.autoSize = TextFieldAutoSize.LEFT;
            this._leftTime.mouseEnabled = false;
            this._leftTime.configEventListener(EventName.SecViewTimeChange, this.onSecViewTimeChangeHandler);
            this._line1 = UICompomentPool.getUICompoment(GSprite);
            this._bodySprite.addChild(this._line1);
            this._line2 = UICompomentPool.getUICompoment(GSprite);
            this._bodySprite.addChild(this._line2);
            this._line3 = UICompomentPool.getUICompoment(GSprite);
            this._bodySprite.addChild(this._line3);
            this._moveBtn.configEventListener(MouseEvent.MOUSE_DOWN, this.onMoveBtnMouseHandler);
            StageMouseManager.instance.addRecEvent(this._judgeRec, this.mouseOverModuleHandler, this.mouseOutModuleHandler);
            LoaderManager.instance.load("ChallengeTimeClock" + ".swf", this.resGotHandler);
            LoaderHelp.addResCallBack(ResFileConst.challengeTimePanel, this.showSkin);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            if (this._clip)
            {
                this._clip.stop();
                DisplayUtil.removeMe(this._clip);
                this._clip = null;
            }
            this._bgSprite.dispose(param1);
            this._bgSprite = null;
            this._bodySprite.dispose(param1);
            this._bodySprite = null;
            this._bgImg.dispose(param1);
            this._bgImg = null;
            this._moveBtn.dispose(param1);
            this._moveBtn = null;
            this._btnShowPanel.dispose(param1);
            this._btnShowPanel = null;
            this._leftTime.dispose(param1);
            this._leftTime = null;
            this._line1.dispose(param1);
            this._line1 = null;
            this._line2.dispose(param1);
            this._line2 = null;
            this._line3.dispose(param1);
            this._line3 = null;
            this._title.dispose(param1);
            this._title = null;
            this._timeTxt.dispose(param1);
            this._timeTxt = null;
            this._timeBg.dispose(param1);
            this._timeBg = null;
            this._data = null;
            this._isShowSkin = false;
            StageMouseManager.instance.removeRecEvent(this._judgeRec);
            this.killTween();
            if (this._tweenPointer)
            {
                this._tweenPointer.kill();
            }
            return;
        }// end function

        private function resGotHandler(param1:SWFInfo) : void
        {
            if (_disposed)
            {
                return;
            }
            this._clip = param1.clip;
            this._clip.x = -25;
            this._clip.y = -31;
            this._bodySprite.addChild(this._clip);
            return;
        }// end function

        private function showSkin() : void
        {
            this._isShowSkin = true;
            this._timeBg.bitmapData = GlobalClass.getBitmapData(ImagesConst.ChallengTimeNull);
            this.pushUIToDisposeVec(UIFactory.gBitmap(ImagesConst.ChallengLine_1, -91, -35, this._line1));
            this.pushUIToDisposeVec(UIFactory.gBitmap(ImagesConst.ChallengLine_2, -91, 0, this._line2));
            this.pushUIToDisposeVec(UIFactory.gBitmap(ImagesConst.ChallengLine_3, 0, 0, this._line3));
            this.pushUIToDisposeVec(UIFactory.gBitmap(ImagesConst.PointStar, -45, -27, this._line1));
            this.pushUIToDisposeVec(UIFactory.gBitmap(ImagesConst.PointStar, -50, 0, this._line2));
            this.pushUIToDisposeVec(UIFactory.gBitmap(ImagesConst.PointStar, -75, 0, this._line2));
            this.pushUIToDisposeVec(UIFactory.gBitmap(ImagesConst.PointStar, 25, 2, this._line3));
            this.pushUIToDisposeVec(UIFactory.gBitmap(ImagesConst.PointStar, 50, 2, this._line3));
            this.pushUIToDisposeVec(UIFactory.gBitmap(ImagesConst.PointStar, 75, 2, this._line3));
            this.layerOutLine();
            return;
        }// end function

        public function set data(param1:ChallengeTimeData) : void
        {
            this._data = param1;
            this.layerOutLine();
            return;
        }// end function

        public function set startCount(param1:int) : void
        {
            this._leftTime.startCount(param1);
            return;
        }// end function

        public function stopCount() : void
        {
            if (this._leftTime && this._leftTime.running)
            {
                this._leftTime.stop();
            }
            return;
        }// end function

        private function layerOutLine() : void
        {
            var _loc_1:Point = null;
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            if (this._isShowSkin && this._data)
            {
                _loc_1 = new Point(this._timeBg.x + this._timeBg.width / 2, this._timeBg.y + this._timeBg.height / 2);
                _loc_2 = (this._timeBg.width - 20) / 2;
                this._line1.x = _loc_1.x + 9;
                this._line1.y = _loc_1.y - this._timeBg.height / 2 + 7;
                _loc_3 = Math.sin(this._data.twoStarTime / this._data.oneStarTime * 2 * Math.PI) * _loc_2 + _loc_1.x;
                this._line2.y = (-Math.cos(this._data.twoStarTime / this._data.oneStarTime * 2 * Math.PI)) * _loc_2 + _loc_1.y - 6;
                if (_loc_3 > _loc_1.x)
                {
                    this._line2.x = _loc_3 - 10;
                    this._line2.scaleX = -1;
                }
                else
                {
                    this._line2.x = _loc_3 + 10;
                    this._line2.scaleX = 1;
                }
                this._line3.x = Math.sin(this._data.threeStarTime / this._data.oneStarTime * 2 * Math.PI) * _loc_2 + _loc_1.x - 10;
                this._line3.y = (-Math.cos(this._data.threeStarTime / this._data.oneStarTime * 2 * Math.PI)) * _loc_2 + _loc_1.y - 6;
                this._line1.visible = this._data.oneStarTime != 0;
                this._line2.visible = this._data.twoStarTime != 0;
                this._line3.visible = this._data.threeStarTime != 0;
            }
            return;
        }// end function

        private function showHideTrackHandler(event:MouseEvent) : void
        {
            var _loc_2:Boolean = false;
            if (this._btnShowPanel.isClose)
            {
                _loc_2 = false;
            }
            else
            {
                _loc_2 = true;
            }
            this._bodySprite.visible = _loc_2;
            return;
        }// end function

        private function onSecViewTimeChangeHandler(event:DataEvent) : void
        {
            if (this._timeTxt)
            {
                this._timeTxt.text = this._leftTime.updateCount >= this._data.oneStarTime ? (this._data.oneStarTime.toString()) : (this._leftTime.updateCount.toString());
            }
            if (this._clip)
            {
                this._clip.gotoAndStop(int(this._leftTime.updateCount / this._data.oneStarTime * 100));
            }
            if (this._line1 && this._leftTime.updateCount > this._data.oneStarTime)
            {
                this._line1.filters = [FilterConst.colorFilter2];
            }
            else if (this._line1)
            {
                this._line1.filters = [];
            }
            if (this._line2 && this._leftTime.updateCount > this._data.twoStarTime)
            {
                this._line2.filters = [FilterConst.colorFilter2];
            }
            else if (this._line2)
            {
                this._line2.filters = [];
            }
            if (this._line3 && this._leftTime.updateCount > this._data.threeStarTime)
            {
                this._line3.filters = [FilterConst.colorFilter2];
            }
            else if (this._line3)
            {
                this._line3.filters = [];
            }
            return;
        }// end function

        public function updateLeftTime() : void
        {
            var _loc_2:int = 0;
            var _loc_1:* = Cache.instance.cd.getCDData("ChallengeCopyCD", CDDataType.backPackLock) as CDData;
            if (_loc_1 && this._data && this._leftTime)
            {
                _loc_2 = (this._data.oneStarTime * 1000 - _loc_1.leftTime) / 1000;
                if (_loc_2 > 3600)
                {
                    this._leftTime.setParse(Language.getString(30055));
                }
                else if (_loc_2 <= 3600)
                {
                    this._leftTime.setParse(Language.getString(30056));
                }
                this._leftTime.startCount(_loc_2);
                this._speed = 359 / this._data.oneStarTime;
            }
            else if (this._leftTime)
            {
                this._leftTime.startCount(0);
            }
            return;
        }// end function

        private function mouseOverModuleHandler() : void
        {
            this.killTween();
            this._tween = TweenMax.to(this, 0.3, {trackAlpha:1});
            return;
        }// end function

        private function mouseOutModuleHandler() : void
        {
            this.killTween();
            this._tween = TweenMax.to(this, 6, {trackAlpha:0});
            return;
        }// end function

        protected function killTween() : void
        {
            if (this._tween)
            {
                this._tween.kill();
            }
            return;
        }// end function

        private function onMoveBtnMouseHandler(event:MouseEvent) : void
        {
            this._dragRect.x = 0;
            this._dragRect.y = 0;
            this._dragRect.width = SceneRange.display.width - 30;
            this._dragRect.height = SceneRange.display.height - 30;
            startDrag(false, this._dragRect);
            Global.stage.addEventListener(MouseEvent.MOUSE_UP, this.onStageMouseUpHandler);
            return;
        }// end function

        private function onStageMouseUpHandler(event:MouseEvent) : void
        {
            this.updateJudgeRect();
            stopDrag();
            Global.stage.removeEventListener(MouseEvent.MOUSE_UP, this.onStageMouseUpHandler);
            return;
        }// end function

        private function updateJudgeRect() : void
        {
            this._judgeRec.x = this.x;
            this._judgeRec.y = this.y;
            return;
        }// end function

        public function set trackAlpha(param1:Number) : void
        {
            if (this._bgSprite)
            {
                this._bgSprite.alpha = param1;
            }
            return;
        }// end function

        public function get trackAlpha() : Number
        {
            return this._bgSprite ? (this._bgSprite.alpha) : (0);
        }// end function

        override public function show(param1:int = 0, param2:int = 0) : void
        {
            super.show(param1, param2);
            this.stageResize();
            return;
        }// end function

        override public function stageResize() : void
        {
            super.stageResize();
            x = SceneRange.display.width - this.width - 30;
            y = 200;
            this.updateJudgeRect();
            return;
        }// end function

        public static function get instance() : ChallengeTimePanel
        {
            if (_instance == null)
            {
                _instance = new ChallengeTimePanel;
            }
            return _instance;
        }// end function

        public static function get isViewShow() : Boolean
        {
            return _instance && !_instance.isHide;
        }// end function

    }
}
